import 'react-native-gesture-handler';
import * as React from 'react';
import {NavigationContainer, DefaultTheme} from '@react-navigation/native';
import {createStackNavigator, TransitionPresets} from '@react-navigation/stack';
import * as routes from '../pages';
import {Colors} from 'react-native-ui-lib';
import BottomTabs from './BottomTabs';
import Storage from '@/utils/storage';
import {useRequest} from 'ahooks';
import {navigationRef} from './RootNavigation';

const Stack = createStackNavigator();

const Router = ({appTheme}) => {
  const {data, loading} = useRequest(async () => {
    let token = await Storage.get('token');
    return token;
  });

  return (
    !loading && (
      <NavigationContainer
        ref={navigationRef}
        theme={{
          ...DefaultTheme,
          colors: {
            background: Colors.backgroundColor,
            secondaryContainer: 'transparent',
          },
        }}>
        <Stack.Navigator initialRouteName={data ? 'Homepage' : 'Login'}>
          {Object.keys(routes).map(it => {
            return (
              <Stack.Screen
                key={it}
                name={it}
                component={routes[it]}
                options={({route, navigation}) => ({
                  title: it,
                  headerTransparent: true, //透明底
                  animationEnabled: true,
                  headerShown: false, //是否显示
                  cardShadowEnabled: false,
                  cardOverlayEnabled: false,
                  // cardStyle: {
                  //   backgroundColor: Colors.backgroundColor,
                  // },
                  presentation: 'modal',
                  // headerStyle: {
                  //   height: 44,
                  //   backgroundColor: Colors.backgroundColor,
                  //   shadowOpacity: 0,
                  //   shadowOffset: {
                  //     height: 0,
                  //   },
                  //   shadowRadius: 0,
                  //   elevation: 0,
                  // },
                  //headerBackTitle:"返回",
                  //headerBackTitleVisible:true,
                  // headerTitleAlign: 'center',
                  // headerTintColor: Colors.headingColor,
                  // headerTitleStyle: {
                  //   fontWeight: 'normal',
                  //   fontSize: 16,
                  // },
                  // headerLeft: props => {
                  //   return (
                  //     [''].indexOf(it) == -1 && (
                  //       <AntdIcons
                  //         name={'left'}
                  //         size={20}
                  //         style={{color: '#000', marginLeft: 12}}
                  //         onPress={() => {
                  //           navigation.goBack();
                  //         }}
                  //       />
                  //     )
                  //   );
                  // },
                  ...TransitionPresets.SlideFromRightIOS,
                })}
              />
            );
          })}
          <Stack.Screen
            key={'Homepage'}
            name={'Homepage'}
            component={BottomTabs}
            options={({route, navigation}) => ({
              headerTransparent: true, //透明底
              animationEnabled: true,
              headerShown: false, //是否显示
              cardShadowEnabled: false,
              cardOverlayEnabled: false,
              presentation: 'modal',
              //...TransitionPresets.SlideFromRightIOS,
            })}
          />
        </Stack.Navigator>
      </NavigationContainer>
    )
  );
};

export default Router;
